package be.kuleuven.thesis.pages;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.framework.Song;
import be.kuleuven.thesis.framework.SongDatabase;
import be.kuleuven.thesis.widget.SearchSongAdapter;

public class Search extends Activity{
	EditText searchField;
	ArrayAdapter<Song> songAdapter;
	List<Song> songs = new ArrayList<Song>();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search);
        
        defineUI();
        
        ListView resultList = (ListView)findViewById(R.id.resultList);
        songAdapter = new SearchSongAdapter(this, songs, getResources());
        resultList.setAdapter(songAdapter);
        
        GenerecsTabActivity parentActivity;
        parentActivity = (GenerecsTabActivity) this.getParent();
        parentActivity.setOnTabChangedListener(new OnTabChangeListener() {		
			@Override
			public void onTabChanged(String tabId) {
				InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(searchField.getWindowToken(), 0);
			}
		});
	}
	
	private void defineUI() {
		Button clearButton = (Button)findViewById(R.id.clear_button);
		clearButton.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View view) {
				searchField.setText("");
			}
		});
		
		Button searchButton = (Button)findViewById(R.id.search_button);
		searchButton.setOnClickListener(new View.OnClickListener() {	
			@Override
			public void onClick(View view) {
				search(searchField.getText().toString());
			}
		});
		
		searchField = ((EditText)findViewById(R.id.search_field));
		searchField.setOnKeyListener(new View.OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
						(keyCode == KeyEvent.KEYCODE_ENTER)) {
					search(searchField.getText().toString());
					return true;
				}
				return false;
			}
		});
		searchField.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEARCH) {
					search(searchField.getText().toString());
					return true;
				}
				return false;
			}
		});
	}
	
	private void search(String query) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(searchField.getWindowToken(), 0);
		
		songs.clear();
		songs.addAll(SongDatabase.search(query));
		songAdapter.notifyDataSetChanged();
	}
	
	public void switchTab(int indexTabToSwitchTo){
        GenerecsTabActivity parentActivity;
        parentActivity = (GenerecsTabActivity) this.getParent();
        parentActivity.switchTab(indexTabToSwitchTo);
	}
	
	@Override
	public void onBackPressed() {
		GenerecsTabActivity.tabActivity.back();
		return;
	}
}
